package com.example.aigc_education.integration.response.workflows.v2;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@Slf4j
@JsonDeserialize(using = CozeGenFlashCResponse.CozeCardDataListDeserializer.class)
public class CozeGenFlashCResponse implements Serializable {

    @JsonProperty("Output")
    private String output;

    private List<CardData> cards;


    @Data
    public static class CardData implements Serializable{
        @JsonProperty("card_question")
        private String cardQuestion;
        @JsonProperty("card_answer")
        private String cardAnswer;
        private String interpretation;
    }

    public static final class CozeCardDataListDeserializer extends JsonDeserializer<CozeGenFlashCResponse> {
        @Override
        public CozeGenFlashCResponse deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
            ObjectMapper mapper = (ObjectMapper) p.getCodec();
            JsonNode rootNode = p.readValueAsTree();

            CozeGenFlashCResponse response = new CozeGenFlashCResponse();

            // 处理直接包含questions数组的情况
            JsonNode cardsNode = rootNode.path("cards");
            if (!cardsNode.isMissingNode() && cardsNode.isArray()) {
                response.setCards(parseCardsArray(mapper, cardsNode));
                return response;
            }

            JsonNode outputNode = rootNode.path("Output");
            if (!outputNode.isMissingNode()) {
                String outputText = outputNode.asText();
                response.setOutput(outputText);

                try {
                    // 尝试解析Output字段中的JSON
                    JsonNode outputJsonNode = mapper.readTree(outputText);

                    // 检查是否包含questions数组
                    cardsNode = outputJsonNode.path("cards");
                    if (!cardsNode.isMissingNode() && cardsNode.isArray()) {
                        response.setCards(parseCardsArray(mapper, cardsNode));
                        return response;
                    }
                } catch (Exception e) {
                    log.warn("解析Output字段中的JSON失败: {}", e.getMessage());
                }
            }
            return null;
        }

        private List<CardData> parseCardsArray(ObjectMapper mapper, JsonNode cardsNode) throws JsonProcessingException {
            List<CardData> cardDataList = new ArrayList<>();

            for (JsonNode cardNode : cardsNode) {
                CardData cardData = mapper.treeToValue(cardNode, CardData.class);
                cardDataList.add(cardData);
            }
            return cardDataList;
        }
    }
}
